<template>
  <div>
    <div class="body">
      <div class="top">
        <div @click="all('new')">最新MV<i class="el-icon-arrow-right"></i></div>
        <div class="tag">
          <div v-for="(item, i) in areaData" :key="item.id" @click="changeTag(item)"
            :style="id === i ? 'color:#ec4141' : 'color:#000'">
            {{ item.name }}
          </div>
        </div>
      </div>

      <!-- 最新 -->
      <div class="content" v-if="newData !== null">
        <div v-for="item in newData" :key="item.id" class="contentBox">
          <el-card shadow="always" :body-style="{ padding: '0px' }">
            <div class="count">
              <i class="el-icon-video-play"></i>{{
                  item.playCount >= 10000
                    ? (item.playCount / 10000).toFixed(0) + "万"
                    : item.playCount
              }}
            </div>
            <el-image :src="item.cover" lazy fit="cover" @click="playMv(item)">
              <div slot="placeholder" class="image-slot">
                <el-image src="" lazy @click="playMv(item)"></el-image>
              </div>
            </el-image>
            <div style="padding: 14px">
              <el-tooltip class="item" effect="dark" :content="item.name" placement="top">
                <div class="desc">
                  {{ item.name }}
                </div>
              </el-tooltip>
            </div>
          </el-card>
        </div>
      </div>

      <!-- 网易出品 -->
      <div class="top">
        <div @click="all('wy')">
          网易出品<i class="el-icon-arrow-right"></i>
        </div>
      </div>
      <div class="content" v-if="WmvData !== null">
        <div v-for="item in WmvData" :key="item.id" class="contentBox">
          <el-card shadow="always" :body-style="{ padding: '0px' }">
            <div class="count">
              <i class="el-icon-video-play"></i>{{
                  item.playCount >= 10000
                    ? (item.playCount / 10000).toFixed(0) + "万"
                    : item.playCount
              }}
            </div>
            <el-image :src="item.cover" lazy fit="cover" @click="playMv(item)">
              <div slot="placeholder" class="image-slot">
                <el-image src="" lazy @click="playMv(item)"></el-image>
              </div>
            </el-image>
            <div style="padding: 14px">
              <el-tooltip class="item" effect="dark" :content="item.name" placement="top">
                <div class="desc">
                  {{ item.name }}
                </div>
              </el-tooltip>
            </div>
          </el-card>
        </div>
      </div>

      <!-- 热播 -->
      <div class="top">
        <div @click="all('hot')">热播MV<i class="el-icon-arrow-right"></i></div>
      </div>
      <div class="content" v-if="PerData !== null">
        <div v-for="item in PerData" :key="item.id" class="contentBox">
          <el-card shadow="always" :body-style="{ padding: '0px' }">
            <div class="count">
              <i class="el-icon-video-play"></i>{{
                  item.playCount >= 10000
                    ? (item.playCount / 10000).toFixed(0) + "万"
                    : item.playCount
              }}
            </div>
            <el-image :src="item.picUrl" lazy fit="cover" @click="playMv(item)">
              <div slot="placeholder" class="image-slot">
                <el-image src="" lazy @click="playMv(item)"></el-image>
              </div>
            </el-image>
            <div style="padding: 14px">
              <el-tooltip class="item" effect="dark" :content="item.name" placement="top">
                <div class="desc">
                  {{ item.name }}
                </div>
              </el-tooltip>
            </div>
          </el-card>
        </div>
      </div>

      <!-- 排行榜 -->
      <Rank />
    </div>
    <div class="loading" v-if="loading === true">
      <i class="el-icon-loading"></i>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  components: {
    Rank: () => import('../../components/mvRank.vue'),
  },
  mounted() {
    this.getNew();
    this.getPer();
    this.getWmv();
  },
  data() {
    return {
      area: '内地',
      areaData: [
        { id: 0, name: '内地' },
        { id: 1, name: '港台' },
        { id: 2, name: '欧美' },
        { id: 3, name: '日本' },
        { id: 4, name: '韩国' },
      ],
      newData: null,
      PerData: null,
      WmvData: null,
      id: 0,
      loading: false,
    };
  },
  beforeRouteLeave(to, from, next) {
    this.loading = true
    next()
  },
  methods: {
    ...mapActions(['saveMVurl', 'saveResMv', 'saveMvBes', 'saveArtistsItem']),
    // 获取最新MV
    async getNew() {
      this.$api.firstMv({ area: this.area, limit: 6, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.newData = ret.data;
      });
    },

    // 获取推荐MV
    async getPer() {
      this.$api.perMv({ limit: 6, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.PerData = ret.result;
      });
    },

    // 获取网易出品 mv
    async getWmv() {
      this.$api.wxMv({ limit: 6, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.WmvData = ret.data;
      });
    },

    // 标签切换
    changeTag(e) {
      this.id = e.id;
      this.area = e.name;
      this.getNew();
    },

    // 播放MV
    async playMv(e) {
      // 获取到MV的地址
      this.$api.mvUrl({ id: parseInt(e.id), cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveMVurl({ mvUrl: ret.data.url });
      });
      this.getArtists(e.artistId);
      this.getResMv(e.id);
      this.getItem(e.id);
    },

    // 获取相似MV
    async getResMv(e) {
      this.$api.simiMv({ mvid: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveResMv({ resMv: ret });
      });
    },

    // 获取MV基础信息
    async getItem(e) {
      this.loading = true;
      this.$api.mvDetail({ mvid: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveMvBes({ mvBes: ret });
        this.$router.push(`/mvPlay?id=${e}`);
      });
    },

    // 获取MV作者
    async getArtists(e) {
      window.sessionStorage.setItem('artists', e);
      this.$api.arDetail({ id: e, cookie: localStorage.getItem('cookie') }).then((ret) => {
        this.saveArtistsItem({ artistsItem: ret.data });
      });
    },

    // 跳转到所有MV页面
    all(e) {
      window.sessionStorage.setItem('mv', e);
      this.$router.push('/allMv');
    },
  },
};
</script>

<style scoped>
.body {
  margin-bottom: 40px;
}

.content {
  display: flex;
  flex-wrap: wrap;
}

.contentBox {
  margin-right: 5px;
  position: relative;
}

.count {
  position: absolute;
  z-index: 10;
  width: 100%;
  text-align: right;
  color: #fff;
}

.desc {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.el-card {
  margin-bottom: 10px;
  cursor: pointer;
  width: 300px;
  position: relative;
}

.el-card .el-image {
  width: 300px;
  height: 200px;
}

.top {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  margin-top: 20px;
  cursor: pointer;
}

.tag {
  display: flex;
}

.tag div {
  margin-right: 10px;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
}

.el-icon-loading {
  position: fixed;
  font-size: 50px;
  left: 50%;
  top: 50%;
  color: #ec4141;
}
</style>
